<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="../../lib/innerIframe-include-ext.js"></script>
<script type="text/javascript" src="../../lib/loader-theme.js"></script>
<script type="text/javascript" src="../../lib/ext/locale/ext-lang-zh_CN.js"></script>
<script type="text/javascript" src="../../lib/ext/plugins/DataTip.js"></script>
<title>学生管理</title>
</head>
<script type="text/javascript">
Ext.require(['*']);
Ext.onReady(function(){
    Ext.QuickTips.init();
    Ext.define('webuser', {
        extend: 'Ext.data.Model',
        fields: [
            'id','user_id','name','leave_desc','begin_date','end_date','reson','status_desc','verify_name','verify_comment'
        ],
        idProperty: 'id'
    });
    
    var store = Ext.create('Ext.data.Store', {
        pageSize: 10,
        model: 'webuser',
        remoteSort: true,
        proxy: {
            type: 'ajax',
            method: 'GET',
            url: '/leave/list_teacher',
            reader: {
                type: 'json',
                root: 'data',
                totalProperty: 'count'
            },
            simpleSortMode: true
        },
        sorters: [
            {
                property: 'id'
            }
        ]
    });
    
    var grid = Ext.create('Ext.grid.Panel', {
        id:'my_grid',
        store: store,
        loadMask: true,
        region: 'center',
        selModel: Ext.create('Ext.selection.CheckboxModel'),
        columns: [
            {
                text: "请假ID",
                dataIndex: 'id',
                width: 100
            },
            {
                text: "用户ID",
                dataIndex: 'user_id',
                width: 100,
                hidden: true
            },{
                text: "姓名",
                dataIndex: 'name',
                width: 100
            },{
                text: '请假类型',
                dataIndex: 'leave_desc',
                width: 100
            }
            ,{
                text: '开始时间',
                dataIndex: 'begin_date',
                width: 150
            },{
                text: '结束时间',
                dataIndex: 'end_date',
                width: 150
            },{
                text: '请假事由',
                dataIndex: 'reson',
                width: 200
            },{
                text: '审批状态',
                dataIndex: 'status_desc',
                width: 150
            },{
                text: '审批人',
                dataIndex: 'verify_name',
                width: 150
            },{
                text: '审批意见',
                dataIndex: 'verify_comment',
                width: 200
            }
        ],
        bbar: Ext.create('Ext.PagingToolbar', {
            store: store,
            displayInfo: true,
            displayMsg: '当前显示  {0} - {1} 共  {2} 条',
            emptyMsg: "没有数据显示！",
            beforePageText: "页码",
            afterPageText: "共 {0} 页",
            firstText:"首页",
            lastText:"末页",
            nextText:"下一页",
            prevText:"上一页",
            refreshText:"刷新"
        }),
        tbar: [
            {
                pressed: false,
                icon : '../../img/accept.png',
                text: "批 准",
                disable:false,
                handler: approvalHandler
            },{
                pressed: false,
                icon : '../../img/close.png',
                text: "拒 绝",
                disable:false,
                handler: rejectHandler
            }]
    });
    store.loadPage(1);
    //grid.getStore().addListener(‘load’,handleGridLoadEvent);

    var viewport = Ext.create('Ext.Viewport', {
        id: 'border-example',
        layout: 'fit',
        items: grid
    });


    var addWin;
    var form;
    form = new Ext.FormPanel({
        id: 'myForm',
        url:'/student/add',
        method:'POST',
        frame:true,
        style:'border-width:0 0 0 0;',
        bodyBorder:false,
        bodyPadding: '0 40 0 0',
        layout: 'form',
        plugins: {
            ptype: 'datatip'
        },
        items: [{
                xtype:'hidden',
                name:'id',
                id:'id'
            },{
                xtype     : 'textareafield',  
                grow      : true,  
                name      : 'verify_comment',  
                //fieldLabel: 'Message',
                anchor    : '100%',
                id:  'verify_comment'
        }],
        buttons:[{ 
            text:'保  存',
            icon:'../../img/accept.png',
            handler:function(){
                if(!form.getForm().isValid()){
                     Ext.Msg.alert('信息提示','请检查输入项是否按要求填写！');
                     return;
                }
                form.form.submit({
                    waitMsg:'正在保存数据...',
                    success:function(form,action){
                        if(action.result.success){
                            Ext.Msg.alert('信息提示',JSON.parse(action.response.responseText).msg,function(){
                                store.reload();
                                Ext.getCmp('myForm').getForm().reset();
                                addWin.close();
                            });
                        } else {
                            Ext.Msg.alert('信息提示',JSON.parse(action.response.responseText).msg);
                        }
                    },
                    failure:function(form,action){
                        Ext.Msg.alert('信息提示', JSON.parse(action.response.responseText).msg);
                    }
                });
            }
        },{
            text:'关  闭',
            icon:'../../img/close.png',
            handler:function(){
                this.up('form').getForm().reset();
                addWin.close();
            }
        }]
    });


    function approvalHandler(){
        var records = grid.getSelectionModel().getSelection();
        if(records.length == 0){
            Ext.Msg.alert("信息提示","请选择需要审批的数据！");
            return;
        }
        for(var i = 0; i < records.length; i++)
        {
            Ext.Ajax.request({
                url: '/leave/approval?id='+records[i].get("id"),
                method : 'POST',
                success: function(response, opts) {
                    var data = eval("("+response.responseText+")");
                    Ext.Msg.alert('信息提示',data.msg);
                    store.reload();
                },
                failure: function(response, opts) {
                    var data = eval("("+response.responseText+")");
                    if(!data.success){
                        Ext.Msg.alert('信息提示',data.msg);
                    } else {
                        Ext.Msg.alert("信息提示","系统忙，请稍候再试");
                    }
                }
            });
        }
    }
    
    function rejectHandler(){
        var records = grid.getSelectionModel().getSelection();
        if(records.length > 1){
            Ext.Msg.alert("信息提示","拒绝需要填写事由，请仅选择一条记录！");
            return;
        } 
        if(records.length == 0){
            Ext.Msg.alert("信息提示","请选择需要审批的记录！");
            return;
        }
        form.form.reset();
        if(!addWin){
            addWin = Ext.create('Ext.Window', {
                title: '填写拒绝事由',
                width: 320,
                height: 200,
                bodyPadding: '0 0 0',
                'z-index': 9999999,
                constrainHeader: true,
                layout: 'fit',
                closeAction:'hide',
                items: form
            });
        }
        form.form.loadRecord(records[0]);
        form.getForm().url='/leave/reject';
        addWin.show();
    }
    
    function handleGridLoadEvent(store,records) {

    }

});
</script>
<body>
</body>
</html>
